//
//  BHCommonWebViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/31.
//

import UIKit
import WebKit

class BHCommonWebViewController: BHBaseViewController {

    
    var titleText: String = ""
    var linkUrl: String = ""
    
    lazy var webView: WKWebView = {
        let conf = WKWebViewConfiguration()
        conf.userContentController = WKUserContentController()
        conf.preferences.javaScriptEnabled = true
        conf.selectionGranularity = WKSelectionGranularity.character
        conf.allowsInlineMediaPlayback = true
   
        let web = WKWebView.init(frame: CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight), configuration: conf)
        web.uiDelegate = self
        web.scrollView.bounces = false
        web.navigationDelegate = self
        web.scrollView.contentInsetAdjustmentBehavior = .never
        return web
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        self.addActConfig()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        self.removeActConfig()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.title_label.text = self.titleText
        
        self.view.addSubview(self.webView)
        
        if self.linkUrl.isEmpty == true {
            BHCommonTool.showAlertWith(showVC: self, title: "提示", message: "地址有误，请确认", okBtn: "知道了", cancel: nil) { (_) in
                self.navigationController?.popViewController(animated: true)
            }
        }else {
            let request = URLRequest.init(url: URL.init(string: self.linkUrl)!)
            self.webView.load(request)
        }
        
        let dataStore = WKWebsiteDataStore.default()
        dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), completionHandler: { (records) in
            for record in records{
                WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {
                    
                })
            }
        })
    }
    

  
}


// MARK: 注入
extension BHCommonWebViewController {
    func addActConfig() {

        self.webView.configuration.userContentController.add(self, name: "getToken")
    }
    func removeActConfig()  {
        self.webView.configuration.userContentController.removeScriptMessageHandler(forName: "getToken")
    }
}
// MAKR: js
extension BHCommonWebViewController: WKScriptMessageHandler {
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
    }
}

extension BHCommonWebViewController: WKUIDelegate,WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        
    }
    
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        
           let alertCtrl = UIAlertController(title: "温馨提示", message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: "确定", style: .default) { (action) in
            completionHandler()
           
        }
        alertCtrl.addAction(action)
        self.present(alertCtrl, animated: true, completion: nil)
    }
    
    
    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
        
          let alertCtrl = UIAlertController(title: "温馨提示", message: message, preferredStyle: .alert)
          let okAction = UIAlertAction(title: "确定", style: .default) { (action) in
              completionHandler(true)
          }
          let cancelAction = UIAlertAction(title: "取消", style: .default) { (action) in
              completionHandler(false)
          }
          alertCtrl.addAction(okAction)
          alertCtrl.addAction(cancelAction)

    }
    
    
}
